﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Model;
using Ledict.Utility.Common;
using Ledict.Utility.Msg;

namespace Ledict.ConditionExtract.Frm.NodeTree
{
    /// <summary>
    /// 要素顺序修改界面
    /// </summary>
    public partial class FrmSuitTypeRelIndexConfig : BaseForm
    {
        /// <summary>
        /// 情形情节业务逻辑类
        /// </summary>
        readonly CaseCauseBLL _caseCauseBll = new CaseCauseBLL();
        /// <summary>
        /// 生成ID逻辑类
        /// </summary>
        readonly NewIDBLL _newIdbll = new NewIDBLL();

        readonly XmlLabel_SuitTypeRelBLL _xmlLabelSuitTypeRelBll = new XmlLabel_SuitTypeRelBLL();

        /// <summary>
        /// 页面标签与诉情种类关系列表
        /// </summary>
        public IList<SYS_XmlLabel_SuitTypeRel> SysXmlLabelSuitTypeRels { get; set; }
        /// <summary>
        /// 情形情节ID
        /// </summary>
        public string CcId { get; set; }

        public FrmSuitTypeRelIndexConfig()
        {
            InitializeComponent();
        }

        #region 绑定数据
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmSuitTypeRelIndexConfig_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            string labelType;
            if (rbtnSuit.Checked)
                labelType = "Suit";
            else if (rbtnFact.Checked)
                labelType = "Fact";
            else
                labelType = "Reason";
            //绑定数据
            BindData(CcId, labelType);
        }

        /// <summary>
        /// 通过CCID获取诉请要素关系配置信息
        /// </summary>
        /// <param name="ccId">CCID</param>
        /// <param name="labelType">诉请类型</param>
        private void BindData(string ccId, string labelType)
        {
            dgvSuitRel.AutoGenerateColumns = false;
            var dt = _xmlLabelSuitTypeRelBll.GetSuitTypeRelListByCcid(ccId, labelType, true);
            dgvSuitRel.DataSource = dt;
        }
        #endregion


        /// <summary>
        /// 生成顺序号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSuitTypeRel_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            var rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dgvSuitRel.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dgvSuitRel.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dgvSuitRel.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        #region RadioButton选择效果
        private void rbtnSuit_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void rbtnFact_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void rbtnReason_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
        }

        #endregion

        #region 编辑
        /// <summary>
        /// 开始编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSuitTypeRel_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex != 4)
                return;
        }

        /// <summary>
        /// 结束编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSuitTypeRel_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {
                var showIndex = dgvSuitRel.Rows[e.RowIndex].Cells["ShowIndex"].Value.ToString();
                var labelPath = dgvSuitRel.Rows[e.RowIndex].Cells["LabelXpath"].Value.ToString();
                var labelType = dgvSuitRel.Rows[e.RowIndex].Cells["LabelType"].Value.ToString();

                if (_xmlLabelSuitTypeRelBll.UpdateIndex(CcId, labelPath, labelType, int.Parse(showIndex)) <= 0)
                {
                    MsgBox.Err(ConventionConst.UpdateFailMsg);
                }
            }
        }
        #endregion

        private void FrmSuitTypeRelIndexConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }




    }
}
